package com.ty.tour.common.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.UUID;

/**
 * @author : www
 * @date: 2018-10-24
 * 生成随机字符串的工具类 uuid
 */
public class UUIDUtils {

    public String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取UUID
     *
     * @return 没有“-”的UUID
     */
    public static String getUUID_WithoutSymbol() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getUUID32() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /***
     * SHA加密 生成40位SHA码
     * @param inStr 待加密字符串
     * @return 返回40位SHA码
     */
    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
}
